function modify(gid, flag, index, price) {
    //flag:-2减 -1加 其他修改
    var checker = $("input[type=checkbox]:eq(" + index + ")").prop("checked");
    var allmoney = parseFloat($("#allmoney").html());
    // console.log(checker);
    // console.log(allmoney);
    // console.log(price);
    var m = $(".inputnum:eq(" + index + ")").val();
    var money = 0;
    if (m <= 0) {
        alert("输入的值有误");
    }
    if (m == 1 && flag == -2) {
        alert("数量不能再减了！")
        return false;
    }
    if (flag == 0) {
        //输入数字的情况
        flag = m;
        if (oldNum < m) {
            // console.log(m);
            money += (m - oldNum) * price;
        }
        if (oldNum > m) {
            money -= (oldNum - m) * price;
        }
    } else if (flag == -2) {
        //减一个数量
        money = -price;
    } else if (flag == -1) {
        //加一个数量
        money = price;
    }
    if (checker) {
        $("#allmoney").html(allmoney + money);
    }
    $.ajax({
        url: "/cart",
        data: {
            method: "updateCart",
            gid: gid,
            flag: flag,
        },
        type: "get",
        dataType: "json",
        beforeSend: function (x) {
            x.setRequestHeader("xhr", "ajax")
        },
        success: function (data) {
            $(".inputnum:eq(" + index + ")").val(data.num);
            $(".totalmoney:eq(" + index + ")").html(data.num * price);
            $(".goodsnum:eq(" + index + ")").html(data.num);
        }
    })
}

function deblete(gid, index) {
    $.ajax({
        url: "/cart",
        data: {
            method: "deleteOne",
            gid: gid,
        },
        type: "get",
        dataType: "json",
        beforeSend: function (x) {
            x.setRequestHeader("xhr", "ajax")
        },
        success: function (data) {
            if (data.code == 1) {
                $(".hang:eq(" + index + ")").css("display", "none");
                // alert("ha2");
                alert(data.msg);
            } else if (data.code == -1) {
                // location.href(data.msg);
            }
        }
    });
}

$(function () {
    $("input[type=checkbox]").each(function (index) {
        $(this).change(function () {
            var total = parseFloat($(".totalmoney:eq(" + index + ")").html());
            var all = parseFloat($("#allmoney").html());
            // console.log(all,total);
            // console.log( $(this).prop("checked"));
            if ($(this).prop("checked")) {
                $("#allmoney").html(all + total);
            } else {
                $("#allmoney").html(all - total);
            }
        });
    })

    $("#from").on("submit",function () {
        console.log($(":checkbox:checked").length);

        if ($(":checkbox:checked").length === 0) {
            alert("请至少选择一件商品！");
            return false;
        } else {
            return true;
        }
    })



})

function getValue(index) {
    oldNum = $(".inputnum:eq(" + index + ")").val();
    console.log(oldNum);
}